import { _decorator, Component, Node, Button, Label, Vec3, v3 } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { lm } from '../../LobbyManager';
import { iap_sdk_m } from '../../../../scripts/managers/SDK/IapSDKManager';
import { sys } from 'cc';
import { misc_sdk_m } from '../../../../scripts/managers/SDK/MiscSDKManager';
import { BUILD_NUMBER, SHORT_COMMIT_HASH } from '../../../../scripts/ConfigConst';
import { forkJoin, Observable } from 'rxjs';
import { TweenToStart } from '../../../../scripts/libs/rxjs/cc3/TweenStart';
const { ccclass, property } = _decorator;

@ccclass('SettingsPanel')
export class SettingsPanel extends BasePanel {

    @property(Node)
    mainNode: Node = null;

    @property(Button)
    closeButton: Button = null;

    @property(Button)
    restoreButton: Button = null;

    @property(Button)
    privacyButton: Button = null;

    @property(Button)
    titleDebugButton: Button = null;

    willBind(): void {
        lm.settingsPanelOpened.value = true;
        // this.adaptive();
        this.mainNode.setScale(.5, .5, .5);
    }

    debug_click_count = 0;

    useObserves() {
        return [
            BindButton(this.closeButton).subscribe(this.closeButtonClicked.bind(this)),
            BindButton(this.restoreButton).subscribe(this.restoreButtonClicked.bind(this)),
            BindButton(this.privacyButton).subscribe(this.privacyButtonClicked.bind(this)),
            BindButton(this.titleDebugButton).subscribe(_ => {
                if (this.debug_click_count > 5) {
                    if (sys.isNative) {
                        this.log('infoDic', misc_sdk_m.infoDic.value);
                        lm.frontTipShow(`Version:${misc_sdk_m.getNativeVersion()} Build:${misc_sdk_m.getNativeBuild()} Beta: ${misc_sdk_m.isTestFlightBeta.value}`);
                    } else {
                        lm.frontTipShow(`Build:${BUILD_NUMBER} Hash:${SHORT_COMMIT_HASH}`);
                    }
                }
                this.debug_click_count++;
            }),
        ];
    }

    // adaptive() {
    //     this.mainNode.setScale(v3(Vec3.ONE).multiplyScalar(lm.getAdaptiveScale()));
    // }
    openPanelAnim(): Observable<any> {
        return forkJoin([
            TweenToStart(this.ensureUIOpacity(), .2, { opacity: 255 }, { easing: 'expoOut' }),
            TweenToStart(this.mainNode, .4, { scale: lm.getAdaptiveScaleV3() }, { easing: 'backOut' }),
        ]);
    }

    closePanelAnim(): Observable<any> {
        this.ensureUIOpacity();
        return forkJoin([
            TweenToStart(this.ensureUIOpacity(), .2, { opacity: 0 }, { easing: 'expoIn' }),
            TweenToStart(this.mainNode, .2, { scale: v3(.5, .5, .5) }, { easing: 'backIn' }),
        ]);
    }


    onDestroyView(): void {
        lm.settingsPanelOpened.value = false;
    }

    closeButtonClicked() {
        this.log('closeButtonClicked');
        SettingsPanel.CloseProcess(null, 'close');
    }

    restoreButtonClicked() {
        this.log('restoreButtonClicked');
        iap_sdk_m.restorePurchase();
    }

    privacyButtonClicked() {
        this.log('privacyButtonClicked');
        sys.openURL('https://pogorockgames.com/hikeeprivacy.html')
    }
}

